<?php
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller {
    public function index(){
    	$this->show("1");
    }
    public function login(){
    	$this->display();
    }
    public function loginHandle(){
        if(!IS_POST) $this->error("...");
        if(!$this->check_verify(I('post.verify'))){
            $this->error("验证码错误");
        }        
        $member = M('member');
        $data = array(
            'username' => I('post.username'),
            'password' => I('post.password','','md5'),
        );
        $result = $member->where($data)->find();
        if(!$result){
            $this->error('账号或密码错误！');
        }
        if($result['status'] == '0'){
            $this->error("账号被锁定");
        }
        //更新数据信息
        $data =array( 
            'id' => $result['id'],
            'logintime' => time(),
            'loginip' => get_client_ip(),
        );  
        $member->save($data);  
        //把用登陆信息存到SESSION
        session("uname",$result['username']);
        session("uid",$result['id']);  
        $this->success("登陆成功",__APP__);
    }    
    public function register(){
    	$this->display();
    }
    public function regHandle(){
        if(!IS_POST) $this->error("...");
        if(!$this->check_verify(I('post.verify'))){
            $this->error("验证码错误");
        }        
        $member = D('member');
        $data = array(
            'username'  => I('post.username'),
            'password'  => I('post.password','','md5'),
            'email'     => I('post.email'),
            'fullname'  => I('post.fullname'),
            'tel'       => I('post.tel'),
            'company'   => I('post.company'),
            'address'   => I('post.address'),
            'company'   => I('post.company'),
            'logintime' => time(),
            'regtime'   => time(),
            'loginip'   => get_client_ip(),
        );

        if(!$member->create()){
            $this->error($member->getError());
        }else{

            $uid = $member->add($data);
            //将用户添加至默认组
            M('auth_group_access')->add(array('uid'=>$uid,'group_id'=>C('DEFAULT_GROUP_ID')));
            if($uid){
                $this->assign('success',$data['username']);
                $this->display('finish');
            }else{
               $this->assign('error','注册失败！请重新注册！');
               $this->display('finish'); 
            }
        }
    } 
    public function logout(){
        session(null);
        $this->success("退出成功",__APP__);
    }
    //AJAX 
    public function check(){
    	if(!IS_AJAX) $this->error("...");
        $member =  M(member);
        $username = I('username');
        $email =  I('email');
        if($username){
            $info = $member->where("username='".$username."'")->find();
            if($info){
                $data['info']=0;
                $this->ajaxReturn($data);
            }else{
                $data['info']=1;
                $this->ajaxReturn($data);                
            }
        }
        if($email){
            $info = $member->where("email='".$email."'")->find();
            if($info){
                $data['info']=0;
                $this->ajaxReturn($data);
            }else{
                $data['info']=1;
                $this->ajaxReturn($data);                
            }
        }
    }  
    public function verify(){
        $config =    array(
            'imageH' => '65',
            'imageW' => '200',
            'length'=>'4',
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();

    }
    public function check_verify($code, $id = ''){    
        $verify = new \Think\Verify();   
        return $verify->check($code, $id);
    }
}